//
//  HomeChannelDetailViewController.m
//  TestApplication
//
//  Created by Hanlingyu on 16/9/6.
//  Copyright © 2016年 ltq. All rights reserved.
//

#import "HomeChannelDetailViewController.h"
#import "TAMacro.h"
#import "TextCollectionViewCell.h"
#import "HomeChannelDetailCollectionViewCell.h"
#import "NewActionContent.h"
#import "AFNetworking.h"
#import "HomeChannelDetailModel.h"
#import "HomeChannelDetailContent.h"
@interface HomeChannelDetailViewController ()
<
    UICollectionViewDelegate,
    UICollectionViewDataSource,
    UICollectionViewDelegateFlowLayout
>
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UICollectionView *homeDetailCollectionView;
@property (nonatomic, strong) NewActionContent *currentActionContent;
@property (nonatomic, strong) HomeChannelDetailModel *homeChannelDetailModel;
@end

@implementation HomeChannelDetailViewController

- (void)netWork {
//    p=%7B%22uid%22%3A%2257c01d33a9a36d0621c123f7%22%2C%22pid%22%3A%225723ebe4a9a36d3340a304b6%22%7D
//      %7B%22uid%22%3A%2257c01d33a9a36d0621c123f7%22%2C%22pid%22%3A%2257cedc1ba9a36d926cc123d8%22%7D
//    {"uid":"57c01d33a9a36d0621c123f7","pid":"5723ebe4a9a36d3340a304b6"}
    NSString *userid = @"57c01d33a9a36d0621c123f7";
    NSString *text = [NSString stringWithFormat:@"{\"uid\":\"%@\",\"pid\":\"%@\"}", userid, _currentActionContent.ID];
//    NSLog(@"%@", text);
    NSString *utf8Text = [text stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet alphanumericCharacterSet]];
//    NSLog(@"%@", utf8Text);
    //    NSString *p = [self URLEncodedString:utf8Text];
//    NSString *parmeters = [NSString stringWithFormat:@"c=Post&m=postDetail&p=%7B%22uid%22%3A%2257c01d33a9a36d0621c123f7%22%2C%22pid%22%3A%225723ebe4a9a36d3340a304b6%22%7D&t=t&v=v105", utf8Text];
    
    NSString *parmeters = [NSString stringWithFormat:@"c=Post&m=postDetail&p=%@&t=t&v=v105", utf8Text];
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    NSString *postURLString = @"http://api.zheli.io:9501/";
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html", nil];
    // 设置请求参数的字符串, 自定义
    [manager.requestSerializer setQueryStringSerializationWithBlock:^NSString * _Nonnull(NSURLRequest * _Nonnull request, id  _Nonnull parameters, NSError * _Nullable __autoreleasing * _Nullable error) {
        
        return parmeters;
    }];
    // 正常post发送
    [manager POST:postURLString parameters:parmeters progress:^(NSProgress * _Nonnull uploadProgress) {
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        self.homeChannelDetailModel = [HomeChannelDetailModel modelWithDic:responseObject];

//        [_homeDetailCollectionView reloadData];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"请求失败: %@", error);
    }];
}

- (void)currentCell:(HomeChannelDetailCollectionViewCell *)cell currentIndexPath:(NSIndexPath *)currentIndexPath {
    
    self.currentActionContent = _contentArray[currentIndexPath.row];
    NSString *userid = @"57c01d33a9a36d0621c123f7";
    NSString *text = [NSString stringWithFormat:@"{\"uid\":\"%@\",\"pid\":\"%@\"}", userid, _currentActionContent.ID];
//    NSLog(@"%@", text);
    NSString *utf8Text = [text stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet alphanumericCharacterSet]];
//    NSLog(@"%@", utf8Text);
    //    NSString *p = [self URLEncodedString:utf8Text];
    //    NSString *parmeters = [NSString stringWithFormat:@"c=Post&m=postDetail&p=%7B%22uid%22%3A%2257c01d33a9a36d0621c123f7%22%2C%22pid%22%3A%225723ebe4a9a36d3340a304b6%22%7D&t=t&v=v105", utf8Text];
    
    NSString *parmeters = [NSString stringWithFormat:@"c=Post&m=postDetail&p=%@&t=t&v=v105", utf8Text];
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    NSString *postURLString = @"http://api.zheli.io:9501/";
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html", nil];
    // 设置请求参数的字符串, 自定义
    [manager.requestSerializer setQueryStringSerializationWithBlock:^NSString * _Nonnull(NSURLRequest * _Nonnull request, id  _Nonnull parameters, NSError * _Nullable __autoreleasing * _Nullable error) {
        
        return parmeters;
    }];
    // 正常post发送
    [manager POST:postURLString parameters:parmeters progress:^(NSProgress * _Nonnull uploadProgress) {
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        self.homeChannelDetailModel = [HomeChannelDetailModel modelWithDic:responseObject];
        cell.homeChannelDetailModel = self.homeChannelDetailModel.content;
//        NSLog(@"%@", _homeChannelDetailModel.content.uname);
//        [_homeDetailCollectionView reloadData];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"请求失败: %@", error);
    }];

    
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
//    [self netWork];
//    [self.homeDetailCollectionView reloadData];

}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
//    NSLog(@"%ld", _currentIndexPath.row);
//    NSLog(@"%ld", _contentArray.count);
    
    self.currentActionContent = _contentArray[_currentIndexPath.row];
    [self netWork];
    [self createView];
    [self createNavigationBar];
    
}

- (void)createView {
    
    //    self.tabBarController.tabBar.hidden = YES;
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    layout.itemSize = CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT);
    layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    layout.minimumLineSpacing = 0;
    layout.minimumInteritemSpacing = 0;
    
    self.homeDetailCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT) collectionViewLayout:layout];
    
    _homeDetailCollectionView.pagingEnabled = YES;
    _homeDetailCollectionView.backgroundColor = [UIColor whiteColor];
    _homeDetailCollectionView.delegate = self;
    _homeDetailCollectionView.dataSource = self;
    [self.view addSubview:_homeDetailCollectionView];
    
    
    
    _homeDetailCollectionView.contentOffset = CGPointMake(_currentIndexPath.row * SCREEN_WIDTH, 0);
    [_homeDetailCollectionView registerClass:[HomeChannelDetailCollectionViewCell class] forCellWithReuseIdentifier:@"HomeChannelDetailCollectionViewCellRuese"];

    
}

//- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
//    
//    
//    
//    return CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT * 2);
//    
//}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return _contentArray.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    HomeChannelDetailCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HomeChannelDetailCollectionViewCellRuese" forIndexPath:indexPath];
    [self currentCell:cell currentIndexPath:indexPath];
//    cell.actionContent = _contentArray[indexPath.row];
    
    return cell;
    
}

- (void)createNavigationBar {
    UIButton *backButton = [[UIButton alloc] initWithFrame:CGRectMake(10, 27, 30, 30)];
    [backButton setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
    [self.backView addSubview:backButton];
    [backButton addTarget:self action:@selector(popViewControllerAction:) forControlEvents:UIControlEventTouchUpInside];
    
    self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 44)];
    _titleLabel.center = CGPointMake(SCREEN_WIDTH / 2, 42);
    _titleLabel.textColor = [UIColor whiteColor];
    _titleLabel.textAlignment = 1;
    _titleLabel.text = @"帖子详情";
    [self.backView addSubview:_titleLabel];
}
- (void)popViewControllerAction: (UIBarButtonItem *)barButtonItem {
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
